const Koa = require('koa')
const app = new Koa()

const server = require('http').createServer(app.callback())
const io = require('socket.io')(server)

// 上线用户
const users = {}
io.on('connection', socket => {
  console.log('socket连接成功')
  socket.on('join', (username) => {
    console.log(username, 'join ~')
    users[username] = socket.id
    socket.name = username
  })

  socket.on('msg', (msgInfo) => {
    console.log(msgInfo)
    // 如果对方在线则发送过去，否则入库
    socket.to(users[msgInfo.toUser]).emit('msg', msgInfo)
  })

})



server.listen(3002)